import User.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "uploadImg")
@MultipartConfig(location = "E:\\local-maven\\shiyan_3\\src\\main\\webapp\\userimags")
public class uploadImg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Part part = request.getPart("img");
        String fileName = part.getSubmittedFileName();
        part.write(fileName);
        String str="userimags/"+fileName;
        User user = (User) request.getSession().getAttribute("SELF");
        if(str != null)user.setImg(str);
        request.getSession().setAttribute("SELF", user);
        if(Treatment.uploadimg(user))
        {
            System.out.println("更改头像成功！");
            response.sendRedirect("/user.jsp");
        }
        else {
            System.out.println("更换头像失败！");
            response.sendRedirect("/error.jsp");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
